var pageSize = 10;
var pageNum = 1;
var page;
var laypage;
var list;
var layer;


$(function () {
    $("#menuId").val(sessionStorage.menuId);
    sessionStorage.rmId = sessionStorage.menuId;
    // alert("000"+sessionStorage.menuId);
    layui.use(['laydate','element','laypage','layer'], function(){
        $ = layui.jquery;//jquery
        laydate = layui.laydate;//日期插件
        element = layui.element();//面包导航
        laypage = layui.laypage;//分页
        layer = layui.layer;//弹出层


    });
    setTimeout(function () {
        findRoleList();
    },200);

});



//查询角色列表
function findRoleList(){
    var menuId = $("#menuId").val();
    if(sessionStorage.getItem(menuId+'add') == 1){
        $("#addRole").css('display','inline');
    }

    var params = {
        "type":"query",
        "menuId":menuId,
        "pageSize":pageSize,
        "pageNum":pageNum

    };
    $.ajax({
        type:"post",
        url: "/srole/findRoleList",
        data:params,
        dataType: "json",
        success : function(data){
            if(data.code == 1 && data.code != undefined){
                list = data.staticRoles;
                page = data.page;
                var html = "";
                for(var b in list){

                    html += '<tr><td><input type="checkbox"></td>' +
                        '<td>'+list[b].roleName+'</td>' +
                        '<td>'+list[b].roleNote+'</td>' +
                        '<td>'+list[b].roleCode+'</td>' +
                        '<td><button class="layui-btn layui-btn-normal" onclick="preparation('+b+')">分配权限</button><button class="layui-btn layui-btn-normal" onclick="showUpdateRolePage('+b+')">编辑角色</button></td></tr>'
                }
                $("#bus").html(html);
                $("#total").html("共有数据："+page.total+" 条");
                laypage({
                    cont: 'page'
                    ,pages: page.pages
                    ,first: 1
                    ,curr:pageNum
                    ,last: page.pages
                    ,prev: '<em><</em>'
                    ,next: '<em>></em>'
                    ,jump: function (obj,first) {
                        pageNum = obj.curr;
                        if(!first){
                            findRoleList();
                        }
                    }
                });
            }else if(data == 9999 && data != undefined){
                window.location.href = "/login_toLogin";
            }else if(data == 1111 && data != undefined){
                alert("无权操作");
            }
        }

    });
}

//打开权限分配页面
var preparationIndex;
function preparation(b) {
    sessionStorage.roleId = list[b].roleId;
    preparationIndex = layer.open({
        title: ['分配权限','color: #2299ee;border-bottom: 1px solid;']
        ,type: 2
        ,content: 'preparation.html'
        ,area: ['500px', '550px']
        ,maxmin:true
    });
    sessionStorage.setItem("preparationIndex",preparationIndex);
}
//打开添加角色页面
var index;
function showAddRolePage(){
    var html = '<div  style="padding: 8% 2%;height: 310px;text-align: center">'+
        '<label style="margin-right: 30px">角色名称</label><input id="roleName" placeholder="角色名称"/><br/><br/>'+
        '<label style="margin-right: 30px">角色描述</label><input id="roleNote" placeholder="角色描述"/><br/><br/>'+
        '<label style="margin-right: 19px">角色CODE</label><input id="roleCode" placeholder="角色CODE"/></div><br/><br/>'+
        '<div style="width: 100%;text-align: center;"><button onclick="addRole(this)" class="layui-btn layui-btn-normal" style="margin: 10px auto 20px;width: 200px">提交</button></div>';

    index = layer.open({
        title: ['添加角色','color: #2299ee;border-bottom: 1px solid;']
        ,type: 1
        ,content: html
        ,area: ['500px', '550px']
        ,maxmin:true
    });
}

//添加角色
function addRole(obj) {
    $(obj).attr("disabled",true);
    $(obj).attr("class","layui-btn layui-btn-disabled");
    var roleName = $("#roleName").val();
    var roleNote = $("#roleNote").val();
    var roleCode = $("#roleCode").val();
    if(roleName == ""){
        alert("角色名称不能为空");
        $(obj).attr("disabled",false);
        $(obj).attr("class","layui-btn layui-btn-normal");
        return false;
    }
    if(roleNote == ""){
        alert("角色描述不能为空");
        $(obj).attr("disabled",false);
        $(obj).attr("class","layui-btn layui-btn-normal");
        return false;
    }
    if(roleCode == ""){
        alert("角色CODE不能为空");
        $(obj).attr("disabled",false);
        $(obj).attr("class","layui-btn layui-btn-normal");
        return false;
    }

    var menuId = $("#menuId").val();
    var params = {
        "type":"add",
        "menuId":menuId,
        "roleName":roleName,
        "roleNote":roleNote,
        "roleCode":roleCode

    };
    $.ajax({
        type: "post",
        url: "/srole/addRole",
        data: params,
        dataType: "json",
        success: function (data) {
            if(data.code == 1 && data.code != undefined){
                alert("添加成功");
                layer.close(index);
                findRoleList();
            }else if(data.code == 2 && data.code != undefined){
                alert("添加失败");
                $(obj).attr("disabled",false);
                $(obj).attr("class","layui-btn layui-btn-normal");
            }else if(data == 9999 && data != undefined){
                window.location.href = "/login_toLogin";
            }else if(data == 1111 && data != undefined){
                alert("无权操作");
                $(obj).attr("disabled",false);
                $(obj).attr("class","layui-btn layui-btn-normal");
            }
        }
    });
}
//打开编辑角色页面
var updateRoleIndex;
function showUpdateRolePage(b) {
    var role = list[b];
    var html = '<div  style="padding: 8% 2%;height: 310px;text-align: center">'+
        '<label style="margin-right: 30px">角色名称</label><input id="updateRoleName" value="'+role.roleName+'"/><br/><br/>'+
        '<label style="margin-right: 30px">角色描述</label><input id="updateRoleNote" value="'+role.roleNote+'"/><br/><br/>'+
        '<label style="margin-right: 19px">角色CODE</label><input id="updateRoleCode" value="'+role.roleCode+'"/></div><br/><br/>'+
        // '<input type="hidden" id="addPermission" value="'+role.addPermission+'"/>'+
        // '<input type="hidden" id="delPermission" value="'+role.addPermission+'"/>'+
        // '<input type="hidden" id="changePermission" value="'+role.addPermission+'"/>'+
        // '<input type="hidden" id="queryPermission" value="'+role.addPermission+'"/>'+
        '<div style="width: 100%;text-align: center;"><button onclick="updateRole(this,'+role.roleId+')" class="layui-btn layui-btn-normal" style="margin: 10px auto 20px;width: 200px">提交</button></div>';

    updateRoleIndex = layer.open({
        title: ['编辑角色','color: #2299ee;border-bottom: 1px solid;']
        ,type: 1
        ,content: html
        ,area: ['500px', '550px']
        ,maxmin:true
    });
}
//编辑角色
function updateRole(obj,roleId) {
    $(obj).attr("disabled",true);
    $(obj).attr("class","layui-btn layui-btn-disabled");
    var updateRoleName = $("#updateRoleName").val();
    var updateRoleNote = $("#updateRoleNote").val();
    var updateRoleCode = $("#updateRoleCode").val();
    if(updateRoleName == ""){
        alert("角色名称不能为空");
        $(obj).attr("disabled",false);
        $(obj).attr("class","layui-btn layui-btn-normal");
        return false;
    }
    if(updateRoleNote == ""){
        alert("角色描述不能为空");
        $(obj).attr("disabled",false);
        $(obj).attr("class","layui-btn layui-btn-normal");
        return false;
    }
    if(updateRoleCode == ""){
        alert("角色CODE不能为空");
        $(obj).attr("disabled",false);
        $(obj).attr("class","layui-btn layui-btn-normal");
        return false;
    }

    var menuId = $("#menuId").val();
    var params = {
        "type":"change",
        "menuId":menuId,
        "roleName":updateRoleName,
        "roleNote":updateRoleNote,
        "roleCode":updateRoleCode,
        "roleId":roleId

    };
    $.ajax({
        type: "post",
        url: "/srole/updateRole",
        data: params,
        dataType: "json",
        success: function (data) {
            if(data.code == 1 && data.code != undefined){
                alert("修改成功");
                layer.close(updateRoleIndex);
                findRoleList();
            }else if(data.code == 2 && data.code != undefined){
                alert("修改失败");
                $(obj).attr("disabled",false);
                $(obj).attr("class","layui-btn layui-btn-normal");
            }else if(data == 9999 && data != undefined){
                window.location.href = "/login_toLogin";
            }else if(data == 1111 && data != undefined){
                alert("无权操作");
                $(obj).attr("disabled",false);
                $(obj).attr("class","layui-btn layui-btn-normal");
            }
        }
    });
}